$.fn.zosoftCity = function(options){
	var _self = this;
	var options = options || {};
	//定义4个默认值
	var areaCode =options.areaCode || null;
	var areaProvince = options.province || "请选择";
	var areaCity = options.city || "请选择 ";
	var areaCounty = options.county||"请选择 ";
	var classStyle= options.classStyle || "select";
	//插入3个空的下拉框
	_self.append('<select name="province" id="province"   class='+classStyle+'></select>');
	_self.append('<select name="city" id="city"  class='+classStyle+'></select>');
	_self.append('<select name="county" id="county" class='+classStyle+'></select>');
	//分别获取3个下拉框
	var $province = _self.find("select").eq(0);
	var $city = _self.find("select").eq(1);
	var $county = _self.find("select").eq(2);
	
	//联动控制
	var provinceIndex = 0;
	$province.change(function(){
		provinceIndex = this.selectedIndex;
		$city[0].options.length=0;
		$county[0].options.length=0;
		
		areaProvince,areaCity,areaCounty="请选择";
		$(this).find("option").eq(0).html("<option value=''>"+areaProvince+"</option>");
		$city.append("<option value=''>"+areaCity+"</option>");
		$county.append("<option value=''>"+areaCounty+"</option>");
		
		if(provinceIndex==0){	  //当选择的为 “请选择” 或 未改变时
			$city.append("<option value=''>"+areaCity+"</option>");
			$county.append("<option value=''>"+areaCounty+"</option>");
		}else{
			$.each(zosoft.geter()[provinceIndex-1].child , function(index,data){
				$city.append("<option  value='"+data.code+"'>"+data.name+"</option>");
			});
		}
	});
	$city.change(function(){
		var cityIndex = this.selectedIndex;
		cityIndex  = cityIndex < 1?cityIndex:(cityIndex-1);
		$county[0].options.length=0;
		$county.append("<option value=''>"+areaCounty+"</option>");
		$.each(zosoft.geter()[provinceIndex-1].child[cityIndex].child , function(index,data){
			$county.append("<option value='"+data.code+"'>"+data.name+"</option>");
		})
	});
	var zosoft={
		area:null, //地区对象
		init:function(options){ 	//初始化
			$province[0].options.length=0;
			$city[0].options.length=0;
			$county[0].options.length=0;
			$province.append("<option value=''>"+areaProvince+"</option>");
			$city.append("<option value=''>"+areaCity+"</option>");
			$county.append("<option value=''>"+areaCounty+"</option>");
			$.each(zosoft.geter(), function(index,data){
				$province.append("<option  value='"+data.code+"'>"+data.name+"</option>");
			});
			if(areaCode){  
				$province.find("option[value*="+areaCode.substring(0,2)+"]").attr("selected",true);
				$province.change();
				$city.find("option[value*="+areaCode.substring(2,4)+"]").attr("selected",true);
				$city.change();
				$county.find("option[value*="+areaCode+"]").attr("selected",true);
			}
			return this;
		},
		seter:function(options){
			zosoft.area = options;
    		if(typeof(options)==="string"){
				$.ajaxSettings.async = false;
				$.getJSON(options,function(data){
					$.ajaxSettings.async = true;
					zosoft.area= data;
				});
			}
			sessionStorage.setItem("area",JSON.stringify(zosoft.area));
			return this;
		},
		geter:function(){
    		if(zosoft.area == "null" ||  zosoft.area == null){
    			if(sessionStorage.getItem("area")== "null" |sessionStorage.getItem("area")== null){
    				$.ajaxSettings.async = false;
    				$.getJSON(options.url ||"/admin/common/js/area.json",function(rlt){
    					$.ajaxSettings.async = true;
    					zosoft.area =rlt;
    					sessionStorage.setItem("area",JSON.stringify(zosoft.area));
    				});
    			}else{
    				zosoft.area = JSON.parse(sessionStorage.getItem("area"));
    			}
    		} 
    		return zosoft.area;
		},
		reload:function(){
			if(zosoft.area == "null" | zosoft.area == null ){
    			zosoft.area = sessionStorage.getItem("area")== "null"?{}:JSON.parse(sessionStorage.getItem("area"));
    		}
			zosoft.init(options);
    		return this.area;
		},
		filter:function(options){
			var a = zosoft.area;
    		var option= options.split(",");
    		if(a == "null" | a == null | !options) a=this.geter();
    		var newarea = new Array();
    		$.each(option,function(index,o){
				newarea = newarea.concat($.map(a, function(n){
				  	return n.code.substring(0,2) == o.substring(0,2) ? n : null;
				}));
			});
    		this.area=newarea;
    		this.reload();
    		return this;
		},
		remove:function(options){
			//待实现
		},
		getCityByCode:function(areaCode){
			if(areaCode)return "";
			var city ={};
			var area = zosoft.geter();
			if(area){
				city = $.map($.map($.map(area, function(n) {
					if(n.code == areaCode){
						return n; 
					}else{
						return n.code.substring(0,2) == areaCode.substring(0,2) ? n.child : null;
					}
				}), function(m) {
					if(m.code == areaCode){
						return m; 
					}else{
						return m.code.substring(2,4) == areaCode.substring(2,4) ? m.child : null;
					}
				}),function(o){
					if(o.code == areaCode){
						return o; 
					}else{
						return o.code.substring(4,6) == areaCode.substring(4,6) ? o : null;
					}
				});
			}
			console.dir(city);
			return city[0].name;
		}
	}
	zosoft.init(options);
	return zosoft;
};

